1703D - Double Strings - CodeForces Solution


brute force data structures strings *1100

Please click on ads to support us..

Python Code:

I = input
for _ in range(int(I())):
    n = int(I())
    a = {}
    l = []
    ans = 0
    for i in range(n):
        s = I()
        a[s] = 0
        l.append(s)
    
    for i in a:
        for j in range(1, len(i)):
            x = i[0:j]
            y = i[j:]
            if x in a and y in a:
                a[i] = 1
        for i in l:
        print(a[i], end='')
    print()

C++ Code:

#include <bits/stdc++.h>
// #define int long long
using namespace std;
using ll = long long;
using umap = unordered_map<int,int>;
using pii = pair<int,int>;
ll gcd(ll x,ll y) {return !y?x:gcd(y,x%y);}

const int N = 100010;
map<string, int> mp;
int n;
string a[N];

void solve() {
    cin >> n;
    mp.clear();
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        mp[a[i]]++;
    }
    string ans = "";
    for (int i = 1; i <= n; i ++) {
        int x = a[i].size();
        bool f = 0;
        for (int j = 1; j < x; j++) {
            string s1 = a[i].substr(0, j);
            string s2 = a[i].substr(j, x - j);
            if (mp[s1] && mp[s2]) {
                f = 1;
                break;
            }
        }
        cout << f;
    }
    cout << "\n";
}
int main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int T; cin >> T;
    while(T --) solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

5B - Center Alignment
549A - Face Detection
535B - Tavas and SaDDas
722C - Destroying Array
366A - Dima and Guards
716B - Complete the Word
1461C - Random Events
1627A - Not Shading
141B - Hopscotch
47B - Coins
1466C - Canine poetry
74A - Room Leader
1333D - Challenges in school №41
1475B - New Year's Number
461A - Appleman and Toastman
320B - Ping-Pong (Easy Version)
948A - Protect Sheep
387A - George and Sleep
53A - Autocomplete
1729G - Cut Substrings
805B - 3-palindrome
805C - Find Amir
676C - Vasya and String
1042B - Vitamins
1729F - Kirei and the Linear Function
25D - Roads not only in Berland
1694A - Creep
659F - Polycarp and Hay
1040A - Palindrome Dance
372A - Counting Kangaroos is Fun